#!/usr/bin/python
# LRC Lyric Format On Screen Display (OSD) (c) 2007 hashao
# This file is licensed under the GPL v3 and later.
# $Revision$ by $Author$
# $HeadURL$

__version__ = "0.0.1"
import sys
import lrc
import pyosd

class LrcOsd(pyosd.osd):
    """LRC lyric file OSD player."""
    def __init__(self,font='-*-*-medium-r-normal-*-*-180-*-*-p-*-*-*', 
            colour='#C0D0C0', timeout=10, pos=0, voffset=0, hoffset=200, shadow=0,
            align=pyosd.ALIGN_LEFT, lines=5, encoding=None):

        import locale
        locale.setlocale(locale.LC_ALL, "")

        pyosd.osd.__init__(self, font, colour, timeout, pos, voffset,
                hoffset, shadow, align, lines)
        self.bottom = lines-1
        if not encoding:
            encoding = locale.getpreferredencoding()
        self.lrc = lrc.Lrc(encoding = encoding)
        self.native_encoding = locale.getpreferredencoding()

    def parse_file(self, afile):
        """Parse a lyric file before play it."""
        self.lrc.parse(afile)

    def play_line(self, text):
        """Play a line of lyric with scrolling."""
        if self.native_encoding != self.lrc.encoding:
            text = text.decode(self.lrc.encoding).encode(self.native_encoding)

        self.scroll(1)
        self.display(text, line=self.bottom)

    def play_time(self, atime):
        """Play a line of lyric at the given time."""
        text = self.lrc.get_lyric(atime)
        self.play_line(text)

    def get_iter(self, atime):
        """Get a lyric iterator of the file."""
        return self.lrc.get_iter(atime)

def main():
    import datetime
    import time
    afile = sys.argv[1]
    # FIXME: This encoding is for simplified Chinese only!
    lcd = LrcOsd(encoding="gbk")
    lcd.lrc.elrc='remove'

    lcd.set_timeout(8)

    lcd.set_pos(pyosd.POS_BOT)
    lcd.set_vertical_offset(100)
    lcd.set_horizontal_offset(800)

    #lcd.set_shadow_offset(2)
    #lcd.set_shadow_colour("#90A0A0")

    lcd.set_outline_offset(1)
    #lcd.set_outline_colour("#908080")
    lcd.set_outline_colour("#304050")

    lcd.parse_file(afile)
    liter = lcd.get_iter(datetime.timedelta(0))

    n = 0
    for i in liter:
        lcd.play_line(i[1])
        time.sleep(5)

        # Test dynamic location change.
        n += 1
        if n % 11 == 0:
            lcd.set_horizontal_offset(900)
        elif n% 7 == 0:
            lcd.set_horizontal_offset(600)
    

    
if __name__ == '__main__':
    main()
# vim:ts=8:sw=4:expandtab

